package com.ng.framework.view.desktop.table;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.plaf.basic.BasicTableHeaderUI;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class NGCustomTableHeaderUI extends BasicTableHeaderUI {

	public void paint(Graphics g, JComponent c) {
		if (header.getColumnModel() != null) {
			((NGCustomTableHeader) header).setColumnMargin();

			int column = 0;
			Hashtable<NGColumnGroup, Rectangle> columnGroupsRectangles = new Hashtable<NGColumnGroup, Rectangle>();

			Dimension size = header.getSize();
			Rectangle cellRect = new Rectangle(0, 0, size.width, size.height);
			Enumeration<TableColumn> tableColumns = header.getColumnModel()
					.getColumns();
			while (tableColumns.hasMoreElements()) {
				cellRect.height = size.height;
				cellRect.y = 0;
				TableColumn tableColumn = tableColumns.nextElement();
				Enumeration<Object> columnGroups = Collections
						.enumeration(((NGCustomTableHeader) header)
								.getColumnGroups(tableColumn));
				if (columnGroups != null) {
					int groupHeight = 0;

					while (columnGroups.hasMoreElements()) {
						NGColumnGroup columnGroup = (NGColumnGroup) columnGroups
								.nextElement();
						Rectangle groupRect = columnGroupsRectangles
								.get(columnGroup);

						if (groupRect == null) {
							groupRect = new Rectangle(cellRect);
							Dimension d = columnGroup
									.getSize(header.getTable());
							groupRect.width = d.width;
							groupRect.height = d.height;
							columnGroupsRectangles.put(columnGroup, groupRect);
						}
						paintCell(g, groupRect, columnGroup);

						groupHeight += groupRect.height;
						cellRect.height = size.height - groupHeight;
						cellRect.y = groupHeight;
					}
				}

				cellRect.width = tableColumn.getWidth() + header.getColumnModel().getColumnMargin();

				if (cellRect.intersects(g.getClipBounds())) {
					paintCell(g, cellRect, column);
				}

				cellRect.x += cellRect.width;
				column++;
			}

		}
	}

	private void paintCell(Graphics g, Rectangle cellRect, int columnIndex) {
		TableColumn tableColumn = header.getColumnModel()
				.getColumn(columnIndex);
		TableCellRenderer renderer = tableColumn.getHeaderRenderer();
		if(renderer == null) {
			renderer = header.getDefaultRenderer();
		}
		Component component = renderer.getTableCellRendererComponent(
				header.getTable(), tableColumn.getHeaderValue(), false, false,
				-1, columnIndex);
		rendererPane.add(component);
		rendererPane.paintComponent(g, component, header, cellRect.x,
				cellRect.y, cellRect.width, cellRect.height, true);
	}

	private void paintCell(Graphics g, Rectangle cellRect, NGColumnGroup cGroup) {
		TableCellRenderer renderer = cGroup.getHeaderRenderer();
		Component component = renderer.getTableCellRendererComponent(
				header.getTable(), cGroup.getHeaderValue(), false, false, -1,
				-1);
		rendererPane.add(component);
		rendererPane.paintComponent(g, component, header, cellRect.x,
				cellRect.y, cellRect.width, cellRect.height, true);
	}

	private int getHeaderHeight() {
		int height = 0;

		TableColumnModel columnModel = header.getColumnModel();
		for (int column = 0; column < columnModel.getColumnCount(); column++) {
			TableColumn tableColumn = columnModel.getColumn(column);
			TableCellRenderer renderer = tableColumn.getHeaderRenderer();
			if(renderer == null) {
				renderer = header.getDefaultRenderer();
			}
			Component component = renderer.getTableCellRendererComponent(
					header.getTable(), tableColumn.getHeaderValue(), false,
					false, -1, column);
			int componentHeight = component.getPreferredSize().height;
			
			List<Object> columnGroups = ((NGCustomTableHeader) header).getColumnGroups(tableColumn);
			for(Object columnGroup : columnGroups) {
				componentHeight += ((NGColumnGroup)columnGroup).getSize(header.getTable()).height;
			}
			height = Math.max(height, componentHeight);
		}

		return height;
	}

	private Dimension createHeaderSize(long width) {
		TableColumnModel columnModel = header.getColumnModel();
		width += columnModel.getColumnMargin() * columnModel.getColumnCount();
		if (width > Integer.MAX_VALUE) {
			width = Integer.MAX_VALUE;
		}
		return new Dimension((int) width, getHeaderHeight());
	}

	public Dimension getPreferredSize(JComponent c) {
		long width = 0;
		Enumeration<TableColumn> enumeration = header.getColumnModel().getColumns();
		while (enumeration.hasMoreElements()) {
			width = width + enumeration.nextElement().getPreferredWidth();
		}
		return createHeaderSize(width);
	}

}
